package com.fengzheng.basic.controller;

import cn.hutool.core.bean.BeanUtil;
import com.fengzheng.basic.common.config.response.ResultBean;
import com.fengzheng.basic.model.po.SysMenuPo;
import com.fengzheng.basic.model.dto.SysMenuDto;
import com.fengzheng.basic.model.vo.SysMenuVo;
import com.fengzheng.basic.service.sys.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sys/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @PostMapping("/save")
    public ResultBean save(@RequestBody SysMenuDto sysMenuDto){
        SysMenuPo sysMenuPo = BeanUtil.copyProperties(sysMenuDto, SysMenuPo.class);
        boolean isSuccess = sysMenuService.saveOrUpdate(sysMenuPo);
        if(isSuccess){
            return new ResultBean(sysMenuPo.getId());
        }else{
            return new ResultBean(false);
        }
    }

    @PostMapping("/delete/{id}")
    public ResultBean delete(@PathVariable Long id){ return new ResultBean(sysMenuService.removeById(id));}

    @PostMapping("/info/{id}")
    public SysMenuVo getById(@PathVariable Long id){
        SysMenuPo sysMenuPo = sysMenuService.getById(id);
        return BeanUtil.copyProperties(sysMenuPo,SysMenuVo.class);
    }

    @GetMapping("/getMenu")
    public List<SysMenuVo> getMenu(Long parentId){
        return sysMenuService.selectByParentId(parentId);
    }

    @GetMapping("/tree")
    public List<SysMenuVo> getTree(Long parentId){
        return sysMenuService.getTreeByParentId(parentId);
    }

}
